package main

import (
	"fmt"
	"time"

	"github.com/garyburd/redigo/redis"
)

var network = "tcp"
var address = "127.0.0.1:6379"
var redisPool *redis.Pool

//const eol = '\n'
func main() {
	fmt.Println("欢迎来到XXX聊天室")
	login()
	register()
	redisPool = initRedis()
	chats, err := getChats(redisPool)
	if err != nil {
		fmt.Println(err)
		return
	}
	chatsContent := chats.([]string)
	for i:=0; i<len(chatsContent); i++ {
		if i % 2 == 0 {
			fmt.Println(chatsContent[i])
		}
	}
}

func initRedis() *redis.Pool {
	var redisPool = &redis.Pool{
		MaxIdle:     8,
		MaxActive:   0,
		IdleTimeout: 100,
		Dial: func() (redis.Conn, error) {
			return redis.Dial(network, address)
		},
	}
	return redisPool
}

func getChats(redisPool *redis.Pool) (interface{}, error) {
	conn := redisPool.Get()
	nowTimeStamp := time.Now().Unix()
	chats, err := redis.Strings(conn.Do("ZRANGEBYSCORE", "chats", nowTimeStamp, "+inf", "withscores"))
	if err != nil {
		return nil, err
	}
	return chats, nil
}
